package com.example.demo.controller;


import com.example.demo.entity.dto.ApiTestCaseResultDto;
import com.example.demo.entity.ApiTestCase;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.ApiTestCaseService;
import com.example.demo.utils.PageInfoNew;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/TestCase")
@Api(tags = "测试用例管理")
public class ApiTestCaseController {

    @Resource
    private ApiTestCaseService apiTestCaseService;

    /**
     * @param apiTestCase 测试用例对象
     * @return 返回结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "测试用例添加接口")
    public ResponseInfo<String> insertApiTestCase(@RequestBody ApiTestCase apiTestCase) {
        try {
            if (apiTestCase.getName().equals("")) {
                return ResponseInfo.errorInfo("测试用例名称不能为空");
            } else {
                apiTestCaseService.insertApiTestCase(apiTestCase);
                return ResponseInfo.successInfo("");
            }
        } catch (Exception e) {
            log.error(e.toString());
            return ResponseInfo.errorInfo(e.toString());
        }
    }

    /**
     * @param id 测试用例主键
     * @return 返回测试用例详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "根据id获取测试用例")
    public ResponseInfo<ApiTestCase> detail(@RequestParam(value = "id") Integer id) {
        return ResponseInfo.successInfo(apiTestCaseService.findById(id));
    }

    /**
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @param projectId 项目主键
     * @param name      测试用例名称
     * @return 返回结果
     */
    @GetMapping("/page")
    @ApiOperation(value = "测试用例分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度", required = true, dataType = "int"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "测试用例名称", required = false, dataType = "String"),
    })
    public ResponseInfo<PageInfoNew<ApiTestCase>> findWithPage(int pageNum, int pageSize,
                                                               @RequestParam(value = "projectId", required = false) Integer projectId,
                                                               @RequestParam(value = "name", required = false) String name) {
        return ResponseInfo.successInfo(apiTestCaseService.findByPage(pageNum, pageSize, projectId, name));
    }

    /**
     * @param testCaseId 测试用例主键
     * @param envId      环境主键
     * @return 返回测试用例结果集
     */
    @PostMapping("/run")
    @ApiOperation(value = "执行测试用例接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "testCaseId", value = "测试用例主键", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "envId", value = "环境主键", defaultValue = "0", dataType = "Integer"),
    })
    public ResponseInfo<ApiTestCaseResultDto> runTestCase(@RequestParam(value = "testCaseId") Integer testCaseId, @RequestParam(value = "envId", defaultValue = "0") Integer envId) {
        return ResponseInfo.successInfo(apiTestCaseService.runCase(testCaseId, envId));
    }

    /**
     * @param testCaseId 测试用例集合
     * @return 返回结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "测试用例删除接口")
    public ResponseInfo<String> deleteTestCase(@PathVariable("id") int testCaseId) {
        apiTestCaseService.testCaseDelete(testCaseId);
        return ResponseInfo.successInfo("");
    }

}
